<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户回收站</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal  layui-btn-radius" style="background-color: #d1e3cc"
                lay-event="batchRes">批量恢复
        </button>
        <button class="layui-btn layui-btn-normal  layui-btn-radius" style="background-color: #d1e3cc"
                lay-event="getCheckLength">获取选中数目
        </button>
        <button class="layui-btn layui-btn-normal  layui-btn-radius" style="background-color: #d1e3cc"
                lay-event="isAll">验证是否全选
        </button>

    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-normal  layui-btn-radius data-count-edit" style="background-color: #deec4f"
       lay-event="restore">恢复</a>
    <a class="layui-btn layui-btn-normal  layui-btn-radius data-count-delete" style="background-color: #d7445a"
       lay-event="del">删除</a>
</script>


<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    layui.use(['table', 'util'], function () {
        var table = layui.table
            , util = layui.util;
        table.render({
            skin: 'nob',// 无边框风格
            cellMinWidth: 80,
            elem: '#test'
            , url: 'user/findDeletedPage'/*tpa=https://www.layuiweb.com/test/table/demo1.json*/
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: '编号', sort: true, align: "center"},
                {field: 'username', width: 80, title: '账号', align: "center"},
                {field: 'password', width: 80, title: '密码', align: "center"},
                {field: 'nick', width: 80, title: '昵称', align: "center"},
                {field: 'urls', title: '头像', templet: '#img', align: "center"},
                {
                    field: 'time',
                    title: '注册时间',
                    templet: "<div> {{layui.util.toDateString(d.time,'yyyy-MM-dd') }} </div>",
                    align: 'center'
                },
                {field: 'address', title: '地址', align: "center"},
                {field: 'phone', title: '手机号', align: "center"},
                {field: 'state', width: 80, title: '状态', align: "center"},
                {title: '操作', minWidth: 220, toolbar: '#barDemo', align: "center"}
            ]],
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
                limits: [5, 10, 15, 20, 25],
                limit: 5,
                page: true,
                skin: 'line',
                groups: 1 //只显示 1 个连续页码
                , first: "首页"
                , last: "尾页"
            }
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'batchRes':
                    var data = checkStatus.data;
                    //layer.alert(JSON.stringify(data));
                    var ids = " ";
                    for (var i = 0; i < data.length; i++) {
                        ids += "&ids=" + data[i].id;
                    }
                    $.get("/user/batchRes", ids,
                        function (data) {
                            //alert(data);
                            if ("success" == data) {
                                table.reload("test", {});//重新加载当前表格
                            }
                        });
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            console.log(data);
            console.log(data.id + "-----------------");
            if (obj.event === 'restore') {
                var deleteId = data.id;
                $.get("/user/restoreById", {id: deleteId},
                    function (data) {
                        if ("success" == data) {
                            table.reload("test", {});//重新加载当前表格
                            alert('恢复成功！')
                        }
                    });
            } else if (obj.event === 'del') {
                layer.confirm('燕子，再也不会见面了嘛？', function (index) {
                    obj.del();//删除表结构
                    layer.close(index);

                    var deleteId = data.id;
                    //alert(deleteId);

                    //去数据库中删除（真正的删除）1.物理删除 2.逻辑删除
                    //向后台发送请求（ajax异步请求）
                    $.get("/user/deleteById", {id: deleteId},
                        function (data) {
                            //alert(data);
                            if ("ok" == data) {
                                table.reload("test", {});//重新加载当前表格
                            }
                        });

                });
            }
        });


    });
</script>
<script type="text/html" id="img">
    <div><img src="{{d.urls}}" style="object-fit: scale-down;border-radius: 50px"></div>
</script>
</body>
</body>
<style>
    .layui-table-cell {
        height: auto;
        /* width: auto;
         min-width: 100px;*/
        line-height: 30px;
    }
</style>
</html>